package org.mspring.platform.security.service;

import java.util.List;

import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.security.dao.RoleDao;
import org.mspring.platform.security.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2013-1-11
 */
@Service
@Transactional
public class RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 统计角色中的用户数量
     *
     * @param roleId
     * @return
     */
    public int countUserByRole(long roleId) {
        return roleDao.countUserByRole(roleId);
    }

    /**
     * 根据用户获取该用户的角色
     *
     * @param userId
     * @return
     */
    public List<Role> listRoleByUser(Long userId) {
        return roleDao.listRoleByUser(userId);
    }

    /**
     * 查找可用的角色
     *
     * @return
     */
    public List<Role> listEnabledRole() {
        return roleDao.listEnabledRole();
    }

    /**
     * 根据名称查找
     *
     * @param name
     * @return
     */
    public Role findByName(String name) {
        return roleDao.findByName(name);
    }

    public Role get(long id) {
        return roleDao.get(id);
    }

    public Page<Role> listPage(QueryBuilder queryBuilder, Page<Role> rolePage) {
        return roleDao.listPage(queryBuilder, rolePage);
    }

    /**
     * 删除角色
     *
     * @param roleId
     */

    public void delete(long roleId) {
        roleDao.delete(roleId);
    }

    public Long insert(Role role) {
        role.setEnabled(true);
        return roleDao.insert(role);
    }

    public void update(Role role) {
        roleDao.update(role);
    }
}
